package com.assets.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.assets.R;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/5/7.
 */

public class Public_list_item_Adapter extends BaseAdapter {
    private List<Map<String, Object>> data;
    private LayoutInflater layoutInflater;
    private Context context;

    public Public_list_item_Adapter(Context context, List<Map<String, Object>> data) {
        //传入的data，就是我们要在listview中显示的信息
        this.context = context;
        this.data = data;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public class Info {
        public TextView odd_tv;//单号
        public TextView creator_tv;//创建人
        public TextView sum_tv;//盘点总数
        public TextView state_tv;//状态
        public TextView scope_tv;//盘点范围
        public TextView Date_created_tv;//创建日期
        public TextView Inventory_date_tv;//盘点日期
        public TextView odd;//单号
        public TextView creator;//创建人
        public TextView sum;//盘点总数
        public TextView state;//状态
        public TextView scope;//盘点范围
        public TextView Date_created;//创建日期
        public TextView Inventory_date;//盘点日期
        public TextView User_tv;//申请人
        public TextView Uset_tv_data;//申请人数据
        private LinearLayout ll_odd;
        private LinearLayout ll4;
        private View view;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position).get("state") + "," + data.get(position).get("odd") + "," + data.get(position).get("scope") + "," + data.get(position).get("creator");
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Info info = new Info();
        convertView = layoutInflater.inflate(R.layout.public_list_item, null);
        info.odd_tv = (TextView) convertView.findViewById(R.id.odd_tv);
        info.creator_tv = (TextView) convertView.findViewById(R.id.creator_tv);
        info.sum_tv = (TextView) convertView.findViewById(R.id.sum_tv);
        info.state_tv = (TextView) convertView.findViewById(R.id.state_tv);
        info.scope_tv = (TextView) convertView.findViewById(R.id.scope_tv);
        info.Date_created_tv = (TextView) convertView.findViewById(R.id.Date_created_tv);
        info.Inventory_date_tv = (TextView) convertView.findViewById(R.id.Inventory_date_tv);
        info.odd = (TextView) convertView.findViewById(R.id.odd);
        info.creator = (TextView) convertView.findViewById(R.id.creator);
        info.Date_created = (TextView) convertView.findViewById(R.id.Date_created);
        info.ll_odd = convertView.findViewById(R.id.ll_odd);
        info.view = convertView.findViewById(R.id.view);
        info.Inventory_date = (TextView) convertView.findViewById(R.id.Inventory_date);
        info.scope = (TextView) convertView.findViewById(R.id.scope);
        info.state = (TextView) convertView.findViewById(R.id.state);
        info.sum = (TextView) convertView.findViewById(R.id.sum);
        //设置数据
        String ood_tv = (String) data.get(position).get("odd_tv");
        info.odd_tv.setText(ood_tv);

        String creator_tv = (String) data.get(position).get("creator_tv");
        info.creator_tv.setText(creator_tv);
        String sum_tv = (String) data.get(position).get("sum_tv");
        info.sum_tv.setText(sum_tv);
        String state_tv = (String) data.get(position).get("state_tv");
        info.state_tv.setText(state_tv);
        String scope_tv = (String) data.get(position).get("scope_tv");
        info.scope_tv.setText(scope_tv);
        String Date_created_tv = (String) data.get(position).get("Date_created_tv");
        info.Date_created_tv.setText(Date_created_tv);
        String Inventory_date_tv = (String) data.get(position).get("Inventory_date_tv");
        info.Inventory_date_tv.setText(Inventory_date_tv);
        String odd = (String) data.get(position).get("odd");
        info.odd.setText(odd);
        if (TextUtils.isEmpty(odd)) {
            info.view.setVisibility(View.GONE);
            info.ll_odd.setVisibility(View.GONE);
        } else {
            info.view.setVisibility(View.VISIBLE);
            info.ll_odd.setVisibility(View.VISIBLE);
        }
        String creator = (String) data.get(position).get("creator");
        String allot_admin = (String) data.get(position).get("allot_admin");
        String Repairs_UserId=(String) data.get(position).get("Repairs_UserId");
        if (!TextUtils.isEmpty(allot_admin)){
            info.creator.setText(allot_admin);
        }else if (!TextUtils.isEmpty(Repairs_UserId)){
            info.creator.setText(Repairs_UserId);
        } else {
            info.creator.setText(creator);
        }

        String Date_created = (String) data.get(position).get("Date_created");
        info.Date_created.setText(Date_created);
        if (!TextUtils.isEmpty((String)data.get(position).get("UserId_tv"))){
            info.ll4=convertView.findViewById(R.id.ll_4);
            info.ll4.setVisibility(View.VISIBLE);
            info.User_tv=(TextView) convertView.findViewById(R.id.User_tv);
            info.Uset_tv_data=(TextView) convertView.findViewById(R.id.User_tv_data);
            info.User_tv.setText((String)data.get(position).get("UserId_tv"));
            info.Uset_tv_data.setText((String)data.get(position).get("userId"));
        }
        if (!TextUtils.isEmpty((String)data.get(position).get("Transfer_time_tv"))){
            info.ll4=convertView.findViewById(R.id.ll_4);
            info.ll4.setVisibility(View.VISIBLE);
            info.User_tv=(TextView) convertView.findViewById(R.id.User_tv);
            info.Uset_tv_data=(TextView) convertView.findViewById(R.id.User_tv_data);
            info.User_tv.setText((String)data.get(position).get("UserId_tv"));
            info.Uset_tv_data.setText((String)data.get(position).get("createdat"));

        }
        if (data.get(position).get("Inventory_date") == null) {
            info.Inventory_date.setVisibility(View.GONE);
        }
        info.Inventory_date.setText(data.get(position)
                .get("Inventory_date") == null ? "暂无此人" : data.get(position)
                .get("Inventory_date") + "");
        if (data.get(position)
                .get("scope") == null) {

            info.scope_tv.setVisibility(View.GONE
            );
            info.scope.setVisibility(View.GONE);

        }
        info.scope.setText(data.get(position)
                .get("scope") + "");
        if (TextUtils.isEmpty((String) data.get(position)
                .get("state"))) {
            info.state.setText("无");
        } else {
            info.state.setText((String) data.get(position)
                    .get("state"));
        }

        info.sum.setText((String) data.get(position)
                .get("sum"));
        return convertView;
    }
}
